(function() {
	angular
		.module('ydApp.chat')
		.config(function($stateProvider) {
			$stateProvider.state('tabs.recent-chat', {
				url: '/recent-chat',
				nativeTransitions: null,
				views: {
					'recent-chat-tab': {
						controller: "recentChatCtrl",
						templateUrl: 'views/chats/recent-chat.html'
					}
				}
			});
			$stateProvider.state('tabs.recent-chat-doc', {
				url: '/recent-chat-doc',
				nativeTransitions: null,
				views: {
					'recent-chat-tab': {
						controller: "recentChatCtrl",
						templateUrl: 'views/chats/recent-chat.html'
					}
				}
			});
		})
		.controller("recentChatCtrl", recentChatCtrl)
		.controller("recentChatDocCtrl", recentChatDocCtrl);

	function recentChatCtrl($scope, xhr, $state, localStore) {

		var vm = $scope.vm = {};

		$scope.downloadImg = function(img) {
			return config.api + "HealthInquiry" + img;
		}

		var user = localStore.get("user");

		var userType = localStore.get("userType");

		if (userType == 2) {
			vm.condition = {
				pageIndex: -1,
				//toUser: 1111
				toUser: user.id
			}

			vm.tabList = [];
			$scope.toChat = function(item) {
				$state.go('chat', {
					chatId: item.id,
					state: item.state,
					docHead: item.toUserHeadImgUrl,
					head: item.fromUserHeadImgUrl
				});
			}
			vm.search = function() {
				xhr.get('HealthInquiry/com/call/findByDoctorCondition', vm.condition).then(function(result) {
					vm.tabList = result.data;
				})
			};

			vm.doRefresh = function() {
				vm.tabList = [];
				vm.condition.pageIndex = 0;
				vm.nodata = false;
				vm.load().finally(function() {
					$scope.$broadcast('scroll.refreshComplete');
				});
			}
			vm.loadMore = function() {
				vm.condition.pageIndex++;
				vm.load().then(function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
				});
			}

			vm.load = function() {
				return xhr.get("HealthInquiry/com/call/findByDoctorCondition", vm.condition).then(function(result) {
					if (result.data.length == 0) {
						vm.nodata = true;
					}
					vm.tabList = vm.tabList.concat(result.data);
				})
			}
			$scope.delete = function (item) {
				xhr.get("HealthInquiry/com/call/deleteByDoctor", {
					id: item.id,
					toUser: item.toUser
				}).then(function(result) {
					for (var i = 0; i < vm.tabList.length; i++) {
						if (vm.tabList[i] == item) {
							vm.tabList.splice(i, 1);
							break;
						}
					}
				});
			}
		} else {
			vm.condition = {
				pageIndex: -1,
				//fromUser: 'e3f7e6f0-d01d-4136-86dc-1158b7f4737d'
				fromUser: user.id
			}

			vm.tabList = [];
			$scope.toChat = function(item) {
				$state.go('chat', {
					chatId: item.id,
					state: item.state,
					docHead: item.toUserHeadImgUrl,
					head: item.fromUserHeadImgUrl
				});
			}
			vm.search = function() {
				xhr.get('HealthInquiry/com/call/findByUserCondition', vm.condition).then(function(result) {
					vm.tabList = result.data;
				})
			};

			vm.doRefresh = function() {
				vm.tabList = [];
				vm.condition.pageIndex = 0;
				vm.nodata = false;
				vm.load().finally(function() {
					$scope.$broadcast('scroll.refreshComplete');
				});
			}
			vm.loadMore = function() {
				vm.condition.pageIndex++;
				vm.load().then(function() {
					$scope.$broadcast('scroll.infiniteScrollComplete');
				});
			}

			vm.load = function() {
				return xhr.get("HealthInquiry/com/call/findByUserCondition", vm.condition).then(function(result) {
					if (result.data.length == 0) {
						vm.nodata = true;
					}
					vm.tabList = vm.tabList.concat(result.data);
				})
			}
			
			$scope.delete = function (item) {
				xhr.get("HealthInquiry/com/call/deleteByUser", {
					id: item.id,
					fromUser: item.fromUser
				}).then(function(result) {
					for (var i = 0; i < vm.tabList.length; i++) {
						if (vm.tabList[i] == item) {
							vm.tabList.splice(i, 1);
							break;
						}
					}
				});
			}
		}

	}

	function recentChatDocCtrl($scope, xhr, localStore, $state) {

		var vm = $scope.vm = {};

		$scope.downloadImg = function(img) {
			return config.api + "HealthInquiry" + img;
		}

		var user = localStore.get("user");

		vm.condition = {
			pageIndex: -1,
			//toUser: 1111
			toUser: user.id
		}

		vm.tabList = [];
		$scope.toChat = function(item) {
			$state.go('chat', {
				chatId: item.id,
				state: item.state,
				docHead: item.toUserHeadImgUrl,
				head: item.fromUserHeadImgUrl
			});
		}
		vm.search = function() {
			xhr.get('HealthInquiry/com/call/findByDoctorCondition', vm.condition).then(function(result) {
				vm.tabList = result.data;
			})
		};

		vm.doRefresh = function() {
			vm.tabList = [];
			vm.condition.pageIndex = 0;
			vm.nodata = false;
			vm.load().finally(function() {
				$scope.$broadcast('scroll.refreshComplete');
			});
		}
		vm.loadMore = function() {
			vm.condition.pageIndex++;
			vm.load().then(function() {
				$scope.$broadcast('scroll.infiniteScrollComplete');
			});
		}

		vm.load = function() {
			return xhr.get("HealthInquiry/com/call/findByDoctorCondition", vm.condition).then(function(result) {
				if (result.data.length == 0) {
					vm.nodata = true;
				}
				vm.tabList = vm.tabList.concat(result.data);
			})
		}
	}
})();